$(function(){
    queryUrlsData(1);
    $("#saveKeyInfo").click(function(){
        var keyWord = $("#id-add-keyWord").val();
        if(!keyWord){
            eDialog.alert('请填写关键词');
            return;
        }
        if(keyWord.length > 100){
            eDialog.alert('输入的关键词长度太长，请精简关键词');
            return;
        }
        var url =  WEB_ROOT + "/key/saveKeyWord"; //请求的网址
        var reqParams = {'keyName':keyWord};//请求参数
        $.ajax({
            type: "POST",
            url: url,
            data: reqParams,
            async: false,
            dataType: "json",
            success: function (data) {
                if (data.flag == "success") {
                    window.top.location.href =WEB_ROOT+"/key/pageInit";
                } else {
                    eDialog.alert(data.msg);
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                eDialog.alert('请填写关键词');
            }
        });
    });

    $("#bindGroup").click(function(){
        var groupid = $("input[name='key2Group']:checked").val();
        if(""==groupid || null == groupid){
            eDialog.alert('请选择分组记录!');
            return;
        }
        //查看状态是否为无效，无效记录则不能进行绑定
        var bindState = $("input[name='key2Group']:checked").attr("bindState");
        if("1"==bindState){
            eDialog.alert('此记录已经绑定过!');
            return;
        }
        var keyid = $("#keyid").val();
        if(""==keyid||null==keyid){
            eDialog.alert('没有需要绑定的关键词，请重新进入!');
            return;
        }
        var url =  WEB_ROOT + "/key/bindGroup"; //请求的网址
        var reqParams = {'groupid':groupid,'keyid':keyid};//请求参数
        $.ajax({
            type: "POST",
            url: url,
            data: reqParams,
            async: false,
            dataType: "json",
            success: function (data) {
                if (data.flag == "success") {
                    eDialog.alert("绑定成功!");
                    queryUrlsData(1);
                } else {
                    eDialog.alert(data.msg);
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                eDialog.alert('请填写关键词');
            }
        });
    });

    $("#unBindGroup").click(function(){
        var groupid = $("input[name='key2Group']:checked").val();
        if(""==groupid || null == groupid){
            eDialog.alert('请选择分组记录!');
            return;
        }
        //查看状态是否为无效，无效记录则不能进行绑定
        var bindState = $("input[name='key2Group']:checked").attr("bindState");
        if("1"!=bindState){
            eDialog.alert('此记录没有绑定过，不能进行解绑!');
            return;
        }
        var keyid = $("#keyid").val();
        if(""==keyid||null==keyid){
            eDialog.alert('没有需要绑定的关键词，请重新进入!');
            return;
        }
        var url =  WEB_ROOT + "/key/unBindGroup"; //请求的网址
        var reqParams = {'groupid':groupid,'keyid':keyid};//请求参数
        $.ajax({
            type: "POST",
            url: url,
            data: reqParams,
            async: false,
            dataType: "json",
            success: function (data) {
                if (data.flag == "success") {
                    eDialog.alert("解绑成功!");
                    queryUrlsData(1);
                } else {
                    eDialog.alert(data.msg);
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                eDialog.alert('请填写关键词');
            }
        });
    });
});

function queryUrlsData(pageNo){
    var keyid = $("#keyid").val();
    if("" == keyid || null == keyid){
        eDialog.alert("没有关联的关键词ID，请重新尝试!");
        return;
    }

    var params = {
        keyid:keyid,
        groupName:$.trim($("#groupName").val()),
        bindState:$("#id-bind").val(),
        pageNo:pageNo,
        pageSize:5
    };

    ajaxLoadPage(params,WEB_ROOT+"/key/grouplist",$("#grouplist-div-id"),function(){
        $("#urlPagerId").pager({callback:function(pageNo){
            queryUrlsData(pageNo);
        }});
    });
}